use web_sys::console;
use js_sys::Array;
use serde::Serialize;
use std::fmt::Debug;
use wasm_bindgen::JsValue;

pub struct Console {}

impl Console {

    /// 生成符合输出到js的值
    fn js_value(message: &str) -> Array {
        let value = JsValue::from_str(message);
        Array::from(&value)
    }

    /// 打印信息 - log
    pub fn log(message: &str) {
        let value = Self::js_value(message);
        console::log(&value);
    }

    /// 打印可序列化的对象
    pub fn log_object<T: Serialize + Debug>(object: &T) {
        let message = format!("{:#?}", &object);
        let value = Self::js_value(&message);
        console::log(&value);
    }

    /// 打印信息 - warn
    pub fn warn(message: &str) {
        let value = Self::js_value(message);
        console::warn(&value);
    }

    /// 打印信息 - error
    pub fn error(message: &str) {
        let value = Self::js_value(message);
        console::error(&value);
    }
}